package com.groupbuying.orderproducer.intercepter;

import com.groupbuying.orderproducer.connector.SysConfigFeign;
import com.groupbuying.orderproducer.utils.JSONUtils;
import com.groupbuying.orderproducer.utils.apiresult.ApiResult;
import com.groupbuying.orderproducer.utils.apiresult.ApiResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 功能开启关闭限制
 * @Author Vincentxin
 * @Date 2019-06-24
 */
@Component
public class FunctionInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    private SysConfigFeign sysConfigFeign;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path = request.getRequestURI();
        try{
            List<String> urls = sysConfigFeign.getUrlByCenterCode("order");
            if (urls.contains(path)){
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().print(JSONUtils.beanToJson(ApiResult.failure(ApiResultCode.NOT_ACCEPTABLE)));
                return false;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }

}
